package org.lendoo.web.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.pojo.common.ApiResult;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.pojo.dto.CartWithGoodsAndImageDto;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.lendoo.web.entity.Cart;
import org.lendoo.web.pojo.vo.CartVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Cart的controller类
 */
@Api(value = "Cart的controller类", tags = "CartController")
@RestController
@RequestMapping("api/v1/cart")
@Slf4j
public class CartController extends AbstractController {

//    /**
//     * 新增
//     * @param cartDto Cart的dto类
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("新增")
//    @ApiImplicitParam(name = "cartDto", value = "Cart的dto类", required = true, dataType = "CartDto")
//    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> add(@RequestBody CartDto cartDto, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == cartDto) {
//            String message = "调用接口【/api/v1/cart/add时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        Cart cart = new Cart();
//        BeanUtils.copyProperties(cartDto, cart);
//        ServiceResult<Boolean> serviceResult = cartService.add(cart);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }

//    /**
//     * 批量导入
//     * @param cartDtoList Cart的dto类列表
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("批量导入")
//    @ApiImplicitParam(name = "cartDtoList", value = "Cart的dto类列表", required = true, dataType = "List")
//    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> batchAdd(@RequestBody List<CartDto> cartDtoList, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == cartDtoList) {
//            String message = "调用接口【/api/v1/cart/batchAdd时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        List<Cart> cartList = new ArrayList<>();
//        //cartList = DtoUtil.cartDtoListToCartList(cartDtoList, cartList);
//        ServiceResult<Boolean> serviceResult = cartService.batchAdd(cartList);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }

    /**
     * 根据id更新
     *
     * @param cartVo  Cart的dto类
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("根据id更新")
    @ApiImplicitParam(name = "cartDto", value = "Cart的dto类", required = true, dataType = "CartDto")
    @RequestMapping(value = "/updateForId", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<Boolean>> updateForId(@RequestBody CartVo cartVo, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == cartVo || null == cartVo.getId()) {
            String message = "调用接口【/api/v1/cart/updateForId时，参数不合法，id不能为空";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        Cart cart = new Cart();
        BeanUtils.copyProperties(cartVo, cart);
        ServiceResult<Boolean> serviceResult = cartService.updateForId(cart);

        apiResult.setSuccess(serviceResult.getSuccess());
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }

    /**
     * 根据id删除
     * @param id 主键
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("根据id删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<Boolean>> deleteById(@PathVariable("id") Integer id, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == id) {
            String message = "调用接口【/api/v1/cart/deleteById时，参数不合法";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        ServiceResult<Boolean> serviceResult = cartService.deleteById(id);

        apiResult.setSuccess(serviceResult.getSuccess());
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }

    /**
     * 根据id查询
     * @param id 主键
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("根据id查询")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
    @RequestMapping(value = "/selectById/{id}", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<Cart>> selectById(@PathVariable("id") Integer id, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == id) {
            String message = "调用接口【/api/v1/cart/selectById时，参数不合法";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        ServiceResult<Cart> serviceResult = cartService.selectById(id);

        apiResult.setSuccess(Boolean.TRUE);
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }

    /**
     * 根据条件，分页查询，升序/降序排列
     *
     * @param cartVo  Cart的dto类
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("根据条件，分页查询，升序/降序排列")
    @ApiImplicitParam(name = "cartDto", value = "Cart的dto类", required = true, dataType = "CartDto")
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<PageInfo<List<CartWithGoodsAndImageDto>>>> page(@RequestBody CartVo cartVo, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == cartVo) {
            String message = "调用接口【/api/v1/cart/page时，参数不合法";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }
        if (null == cartVo.getPageNo() || null == cartVo.getPageSize()) {
            String message = "调用接口【/api/v1/cart/page时，参数pageNo和pageSize不能为空";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        Cart cart = new Cart();
        BeanUtils.copyProperties(cartVo, cart);
        ServiceResult<PageInfo<List<CartWithGoodsAndImageDto>>> serviceResult = cartService.page(cart, cartVo.getPageNo(), cartVo.getPageSize());

        apiResult.setSuccess(Boolean.TRUE);
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }

//    /**
//     * 查询所有记录
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("查询所有记录")
//    @ApiImplicitParam(name = "", value = "", required = true, dataType = "")
//    @RequestMapping(value = "/findAll", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<List<Cart>>> findAll(HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//
//        ServiceResult<List<Cart>> serviceResult = cartService.findAll();
//
//        apiResult.setSuccess(Boolean.TRUE);
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }

    /**
     * 如果购物车中这个用户的这个商品已经存在，则加一；如果不存在则创建这条记录
     *
     * @param cartVo
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("如果购物车中这个用户的这个商品已经存在，则加一；如果不存在则创建这条记录")
    @ApiImplicitParam(name = "cartDto", value = "Cart的dto类", required = true, dataType = "CartDto")
    @RequestMapping(value = "/saveOrIncrementCart", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<Boolean>> saveOrIncrementCart(@RequestBody CartVo cartVo, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == cartVo) {
            String message = "调用接口【/api/v1/cart/saveOrIncrementCart时，参数不合法";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        Cart cart = new Cart();
        BeanUtils.copyProperties(cartVo, cart);
        ServiceResult<Boolean> serviceResult = cartService.saveOrIncrementCart(cart);

        apiResult.setSuccess(serviceResult.getSuccess());
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }
}
